package com.openness.example.lock;

/**
 * @author Rain
 * @version 2013-5-23 下午7:01:10
 */

public class JavaLock {
	
	public static class Lock{
		public synchronized void lock() {
			System.out.println(Thread.currentThread().getName() + " lock");
		}

		public synchronized void unlock() {
			System.out.println(Thread.currentThread().getName() + " unlock");
		}
	}

	public static class Test {
		
		private Lock lock;
		
		public Test(Lock lock){
			this.lock = lock;
		}
		
		public void test(){
			lock.lock();
			
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
			}
			
			lock.unlock();
		}
	}
	
	public static class TestThread extends Thread{
		
		private Test t;
		
		public TestThread(Test t){
			this.t = t;
		}
		
		@Override
		public void run() {
			while(true){
				t.test();
			}
		}
		
	}

	public static void main(String[] args) {
		Test t = new Test(new Lock());
		
		for(int i=0;i<5;i++){
			new TestThread(t).start();
		}
	}

}
